<?php

class HostMetaXRD {
    
    private $host;
    private $lrddTemplate;
    
    public function __construct($host, $lrddTemplate) {
        $this->setHost($host);
        $this->setLrddTemplate($lrddTemplate);
    }
    
    private function setHost($host) {
        if(empty($host)) {
            throw new Exception("Host of the XRD cannot be empty.");
        }
        $this->host = $host;
    }
    
    public function getHost() {
        return $this->host;
    }
    
    private function setLrddTemplate($lrddTemplate) {
        if(empty($lrddTemplate)) {
            throw new Exception("LRDD template of the XRD cannot be empty.");
        }
        $this->lrddTemplate = $lrddTemplate;
    }
    
    public function getLrddTemplate() {
        return $this->lrddTemplate;    
    }
    
    /**
     * Builind funrgion will try to create HostMetaXRD object from XML.
     * 
     * Example XML expected:
     * <?xml version="1.0" encoding="UTF-8"?>
	 * <XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0" xmlns:hm="http://host-meta.net/xrd/1.0">
	 *    <hm:Host>identi.ca</hm:Host>
     *    <Link rel="lrdd" template="http://identi.ca/main/xrd?uri={uri}">
     *       <Title>Resource Descriptor</Title>
     *    </Link>
     * </XRD>
     * 
     * @param String $rawXml
     * @return HostMetaXRD
     */
    public static function buildFromRawXML($rawXml) {
        $dom = new DOMDocument();
        if(@$dom->loadXML($rawXml)) {
            return self::buildFromDom($dom);
        }
        throw new Exception("Invalid raw XML data provided!");
    }
    
    public static function buildFromDom(DOMDocument $dom) {
        
        $xrd_element = $dom->getElementsByTagName('XRD')->item(0);
        if (!$xrd_element) {
            throw new Exception("Invalid DOM object for XRD, missing XRD root");
        }

        // Check for host-meta host
        $host = $xrd_element->getElementsByTagName('Host')->item(0);
        if (!$host) { 
            throw new Exception("Invalid DOM object for XRD, missing host value.");
        }    
        $host = $host->nodeValue;
        
        //$links = $xrd_element->getElementsByTagName('link')
        $lrddTemplate = "";
        foreach ($xrd_element->getElementsByTagName('Link') as $element) {
            if($element->getAttribute('rel') == "lrdd") {
                $lrddTemplate = $element->getAttribute('template');      
            }  
        }
        if(empty($lrddTemplate)) {
            throw new Exception("Invalid DOM object for XRD: template attribute not found from link elements.");           
        }
        return new HostMetaXRD($host, $lrddTemplate);
    }
    
    // These two function should propably be somewhere else.
    public static function generateAcctUri($jid) {
        return urlencode("acct:" . $jid);
    }
    
    /**
     * The static function is used to change the template http://identi.ca/main/xrd?uri={uri}
     * with $jid (pamela@gmail.com) to something like http://identi.ca/main/xrd?uri=acct:pamela@gmail.com
     * 
     * @param string $jid
     * @param string $templateURL
     * @return string $uri 
     */
    public static function generateUserXrdURL($jid, $templateURL) {
        return str_replace("{uri}", self::generateAcctUri($jid), $templateURL);        
    }
}